char switchmap[22][23],blockmap[22][23],doormap[22][23],*p;
stack[9999];
switchx,switchy;
blockx,blocky;
doorx,doory;
main(switch_access,block,door,n,m,i,j,x,y,w,t)
{
	memset(*switchmap,'#',22);
	strcpy(*blockmap,*switchmap);
	strcpy(*doormap,*switchmap);

	for(gets(&n);~scanf("%d%d\n",&n,&m);puts(switch_access&block&door?"YES":"NO"))
	{
		for(i=1;i<=n;i++)
		{
			*switchmap[i]='#';
			gets(switchmap[i]+1);
			strcpy(switchmap[i]+strlen(switchmap[i]),"#");
			p=strchr(switchmap[i],'P');
			p?*p='0':0;
			strcpy(blockmap[i],switchmap[i]);
			strcpy(doormap[i],switchmap[i]);
			j=strchr(switchmap[i],'S');
			j?switchy=i,switchx=switchmap[i],switchx=j-switchx:0;
			j=strchr(blockmap[i],'B');
			j?blocky=i,blockx=blockmap[i],blockx=j-blockx:0;
			j=strchr(doormap[i],'D');
			j?doory=i,doorx=doormap[i],doorx=j-doorx:0;
		}
		memset(switchmap[i],'#',22);
		strcpy(blockmap[i],switchmap[i]);
		strcpy(doormap[i],switchmap[i]);

		*stack=switchx;stack[1]=switchy;
		for(w=t=switch_access=0;!switch_access&&~w;t=0)
		{
			x=stack[2*w];y=stack[2*w--+1];
			
			switch_access=isdigit(switchmap[y+1][x])|isdigit(switchmap[y-1][x])|isdigit(switchmap[y][x+1])|isdigit(switchmap[y][x-1]);
			switchmap[y][x]='#';
			if(switchmap[y+1][x]-'#')stack[++w*2]=x,stack[w*2+1]=y+1,switchmap[y+1][x]='#';
			if(switchmap[y-1][x]-'#')stack[++w*2]=x,stack[w*2+1]=y-1,switchmap[y-1][x]='#';
			if(switchmap[y][x+1]-'#')stack[++w*2]=x+1,stack[w*2+1]=y,switchmap[y][x+1]='#';
			if(switchmap[y][x-1]-'#')stack[++w*2]=x-1,stack[w*2+1]=y,switchmap[y][x-1]='#';
		}
		*stack=blockx;stack[1]=blocky;
		for(w=t=block=0;!block&&~w;t=0)
		{
			x=stack[2*w];y=stack[2*w--+1];
			
			block=isdigit(blockmap[y+1][x])|isdigit(blockmap[y-1][x])|isdigit(blockmap[y][x+1])|isdigit(blockmap[y][x-1]);
			blockmap[y][x]='#';
			if(blockmap[y+1][x]-'#')stack[++w*2]=x,stack[w*2+1]=y+1,blockmap[y+1][x]='#';
			if(blockmap[y-1][x]-'#')stack[++w*2]=x,stack[w*2+1]=y-1,blockmap[y-1][x]='#';
			if(blockmap[y][x+1]-'#')stack[++w*2]=x+1,stack[w*2+1]=y,blockmap[y][x+1]='#';
			if(blockmap[y][x-1]-'#')stack[++w*2]=x-1,stack[w*2+1]=y,blockmap[y][x-1]='#';
		}
		*stack=doorx;stack[1]=doory;
		for(w=t=door=0;!door&&~w;t=0)
		{
			x=stack[2*w];y=stack[2*w--+1];
			
			door=isdigit(doormap[y+1][x])|isdigit(doormap[y-1][x])|isdigit(doormap[y][x+1])|isdigit(doormap[y][x-1]);
			doormap[y][x]='#';
			if(doormap[y+1][x]-'#')stack[++w*2]=x,stack[w*2+1]=y+1,doormap[y+1][x]='#';
			if(doormap[y-1][x]-'#')stack[++w*2]=x,stack[w*2+1]=y-1,doormap[y-1][x]='#';
			if(doormap[y][x+1]-'#')stack[++w*2]=x+1,stack[w*2+1]=y,doormap[y][x+1]='#';
			if(doormap[y][x-1]-'#')stack[++w*2]=x-1,stack[w*2+1]=y,doormap[y][x-1]='#';
		}
	}
}
